package com.javacoo.xservice.base;

import javax.servlet.http.HttpServletResponse;

import com.javacoo.xservice.base.support.HttpCode;
import com.javacoo.xservice.base.support.swaparea.SwapAreaUtils;
import com.javacoo.xservice.base.utils.WebUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 控制器基类
 * <p>说明:</p>
 * <li>接口处理公共方法</li>
 * @author DuanYong
 * @since 2017年6月28日下午2:48:27
 */
@Slf4j
public abstract class BaseController {
	/**
	 * 设置成功响应代码
     * <li></li>
     * @author DuanYong
     * @since 2017年6月28日下午2:50:45
	 * @param httpServletResponse: Response对象
	 * @param data: 业务返回数据
	 * @return: boolean 是否成功
	 */
	protected boolean setSuccessResponse(HttpServletResponse httpServletResponse,Object data) {
		//设置响应数据
		try{
            BaseResponse response = BaseResponse.builder()
                .code(String.valueOf(HttpCode.OK.value()))
                .data(data)
                .message(HttpCode.OK.msg())
                .timestamp(System.currentTimeMillis())
                .transactionSn(WebUtil.getSwapAreaData().getTransactionSn())
                .build();
            //设置响应数据
            WebUtil.getSwapAreaData().setEndTime(System.currentTimeMillis());
            WebUtil.getSwapAreaData().setBaseResponse(response);
			return true;
		}catch (Exception e){
			e.printStackTrace();
            log.error("接口->{},返回异常,流水号:{}", SwapAreaUtils.getSwapAreaData().getReqMethod(),SwapAreaUtils.getSwapAreaData().getTransactionSn(),e);
			return false;
		}
	}
}
